package addr

import (
	"net"

	"gitee.com/segeln/grit/log"
)

func ExtractIP() (string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		log.Errorln("InterfaceAddrs failed,error:", err)
		return "", err

	}
	address := ""
	for _, addr := range addrs {
		// 检查ip地址判断是否回环地址
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			//fmt.Println(ipnet.IP.String())
			if ipnet.IP.To4() != nil {
				address = ipnet.IP.String()
			}
		}
	}
	return address, nil
}
